﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class WaveFlowBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IWaveFlowDA waveFlowDA = DataAccess.CreateWaveFlowDA();
        private static readonly IWaveFlowDetailDA waveFlowDetailDA = DataAccess.CreateWaveFlowDetailDA();

        public WaveFlowBLL()
        {
            this.pubParasInfo = null;
        }

        public WaveFlowBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public WaveFlowBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteWaveFlowInfo(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<WaveFlowInfo> waveFlowInfoList = new List<WaveFlowInfo>();
                WaveFlowInfo waveFlowInfo = null;
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                foreach (string id in IDS)
                {
                    waveFlowInfo = waveFlowDA.GetWaveFlowByID(dataBase, tran, id, this.dataBaseID[0]);
                    waveFlowInfoList.Add(waveFlowInfo);
                }
                if (waveFlowDA.DeleteWaveFlow(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<WaveFlowInfo>(dataBase, tran, waveFlowInfoList[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "WAVE_FLOW", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                    waveFlowDetailDA.DeleteWaveFlowDetail(dataBase, tran, IDS, this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllWaveFlowInfo()
        {
            return waveFlowDA.GetAllWaveFlow(this.dataBaseID[0]);
        }

        public WaveFlowInfo GetWaveFlowInfoByID(string id)
        {
            WaveFlowInfo waveFlowInfo = null;
            try
            {
                waveFlowInfo = waveFlowDA.GetWaveFlowByID(id, this.dataBaseID[0]);
                if (waveFlowInfo != null)
                {
                    waveFlowInfo.WaveFlowDetailInfos = waveFlowDetailDA.GetWaveFlowDetailListByID(id, this.dataBaseID[0], this.pubParasInfo);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return waveFlowInfo;
        }

        public DataSet GetWaveFlowInfoByQueryEntity(WaveFlowQueryEntity waveFlowQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(waveFlowQueryEntity.WaveFlowID))
            {
                sqlWhere.Add(" WAVE_FLOW_ID " + waveFlowQueryEntity.WaveFlowID);
            }
            if (!string.IsNullOrEmpty(waveFlowQueryEntity.Descr))
            {
                sqlWhere.Add(" DESCR " + waveFlowQueryEntity.Descr);
            }
            if (!string.IsNullOrEmpty(waveFlowQueryEntity.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE='" + waveFlowQueryEntity.IsActive + "'");
            }
            return waveFlowDA.GetWaveFlowByQueryList(sqlWhere, waveFlowQueryEntity, this.dataBaseID[0]);
        }

        public string InsertWaveFlowInfo(WaveFlowInfo waveFlowInfo)
        {
            if (!string.IsNullOrEmpty(waveFlowInfo.WaveFlowID) && waveFlowDA.CheckWaveFlowIDUnique(waveFlowInfo.WaveFlowID, this.dataBaseID[0]))
            {
                throw new Exception("此波次流程已存在,请重新输入或为空系统自动生成!");
            }
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strWaveFlowID = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(waveFlowInfo.WaveFlowID))
                {
                    waveFlowInfo.WaveFlowID = commonMethod.GenNextNumStr(dataBase, tran, "WAVE_FLOW", whLoginID);
                }
                waveFlowInfo.CreatedBy = this.pubParasInfo.UserID;
                waveFlowInfo.UpdatedBy = waveFlowInfo.CreatedBy;
                if (waveFlowDA.InsertWaveFlow(dataBase, tran, waveFlowInfo, whLoginID) > 0)
                {
                    strWaveFlowID = waveFlowInfo.WaveFlowID;
                    if ((waveFlowInfo.WaveFlowDetailInfos != null) && (waveFlowInfo.WaveFlowDetailInfos.Count > 0))
                    {
                        foreach (WaveFlowDetailInfo info in waveFlowInfo.WaveFlowDetailInfos)
                        {
                            info.CreatedBy = this.pubParasInfo.UserID;
                            info.UpdatedBy = info.CreatedBy;
                            info.WaveFlowID = strWaveFlowID;
                            waveFlowDetailDA.InsertWaveFlowDetail(info, whLoginID);
                        }
                    }
                    new AuditDataBLL().InsertAuditData<WaveFlowInfo>(dataBase, tran, null, waveFlowInfo, AuditAction.Insert, this.pubParasInfo.UserID, "WAVE_FLOW", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strWaveFlowID;
        }

        public string UpdateWaveFlowInfo(WaveFlowInfo waveFlowInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strWaveFlowID = string.Empty;
            try
            {
                WaveFlowInfo oldWaveFlowInfo = waveFlowDA.GetWaveFlowByID(dataBase, tran, waveFlowInfo.WaveFlowID, this.dataBaseID[0]);
                if (waveFlowDA.UpdateWaveFlow(dataBase, tran, waveFlowInfo, whLoginID) > 0)
                {
                    strWaveFlowID = waveFlowInfo.WaveFlowID;
                    new AuditDataBLL().InsertAuditData<WaveFlowInfo>(dataBase, tran, oldWaveFlowInfo, waveFlowInfo, AuditAction.Update, this.pubParasInfo.UserID, "WAVE_FLOW", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strWaveFlowID;
        }
    }
}

